package com.sondon.mayi.adapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.activity.CartActivity_;
import com.sondon.mayi.activity.GoodInfoActivity_;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.GoodInfo;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.ImageLoaderUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.MyAlertDialog;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.adapter
 * @Class : GoodsList_Item
 * @Author : 蔡文锋
 * @DateTime：2015年6月10日 下午9:24:28
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {}
 *
 */
@EViewGroup(R.layout.goodslist_item)
public class GoodsList_Item extends LinearLayout {

	private Context context;
	@ViewById
	ImageView pic;// 图片
	@ViewById
	TextView vip_price;// 会员价
	@ViewById
	TextView fruit_name;// 水果名
	@ViewById
	TextView sell_price;// 售价
	@ViewById
	TextView market_price;// 市场价
	@ViewById
	TextView store_num;// 库存
	@ViewById
	LinearLayout vip_layout;// VIP布局
	@ViewById
	LinearLayout add2cart_box;// 购买
	@ViewById
	LinearLayout goodslist_info_layout;// 物品布局

	public String good_id;
	private   SharedPreferences perference;
	
	public GoodsList_Item(Context context) {
		super(context);
		this.context = context;
		perference=context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
	}

	@AfterViews
	public void initView() {
		// 设置中划线并加清晰
		market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
	}

	@Click({ R.id.add2cart_box, R.id.pic, R.id.goodslist_info_layout })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add2cart_box:
//			ToastUtils.show(context, "加入购物车");
			String ukey=perference.getString("Ukey", "");
//			LogUtil.e("GoodsList_Item", "ukey  :"+ukey);
			Add2Cart(ukey);
			break;
		case R.id.pic:
		case R.id.goodslist_info_layout:
			// ToastUtils.show(context, "查看商品详情");
			Intent intent = new Intent(context, GoodInfoActivity_.class);
			intent.putExtra("good_id", this.good_id);
			context.startActivity(intent);
			break;
		default:

			break;
		}
	}

	/**
	 * 
	 * @Author 蔡文锋
	 * @Data_Time 2015年6月18日 上午12:11:22
	 * @Description { 列表里的item 绑定数据 }
	 * @param goodinfo
	 */
	public void bindData(GoodInfo goodinfo) {
		this.good_id=goodinfo.getId();
		ImageLoaderUtil.SetImgView(goodinfo.getImg(), this.pic);
		this.fruit_name.setText(goodinfo.getName());
		this.sell_price.setText(goodinfo.getSell_price());
		this.market_price.setText(goodinfo.getMarket_price());
		this.store_num.setText("库存  :" + goodinfo.getStore_nums());
//		LogUtil.e("goodlistitem", "Vip_price  :" + goodinfo.getVip_price());
		
		if (goodinfo.getGroup_price() != null && !"".equals(goodinfo.getGroup_price()) 
				&& !"0".equals(goodinfo.getGroup_price())) {
			vip_layout.setVisibility(View.VISIBLE);
			vip_price.setText(goodinfo.getGroup_price());
		} else {
			vip_layout.setVisibility(View.GONE);
		}
	}
	
	
	/*
	 * 加入购物车
	 */
	public void Add2Cart(String ukey){
		//判断是否已经登录
		if(ukey==null || "".equals(ukey)){
			ToastUtils.show(context, "请登录");
			return;
		}
		
		RequestParams params=new RequestParams();
		params.put("goods_id", this.good_id);
		params.put("num", 1);
		AsyncHttpUtil.post(URLConfigs.Add2Cart_ukey+ukey, params, 
				new TextHttpResponseHandler() {
					
					@Override
					public void onSuccess(int status, Header[] arg1, String json) {
						LogUtil.e("GoodsList_Item", "Add2Cart_ukey  json  :"+json);
						if(status==200){
							RespondBaseEntity rsbEntity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
							if(rsbEntity.isStatus()){//成功
//								ToastUtils.show(context, rsbEntity.getMsg());
								MyAlertDialog.show(context, "购物车", "商品已成功加入购物车", "继续购物","去购物车");
								//左边按钮监听事件
								OnClickListener	 leftListener=new OnClickListener() {
													
													@Override
													public void onClick(View arg0) {
														if(MyAlertDialog.getMydialog().isShowing()){
															MyAlertDialog.getMydialog().dismiss();
														}
													}
												};
								//右边按钮监听事件
								OnClickListener	 rightListener=new OnClickListener() {
													
													@Override
													public void onClick(View arg0) {
														if(MyAlertDialog.getMydialog().isShowing()){
															MyAlertDialog.getMydialog().dismiss();
															Intent  intent=new Intent(context,CartActivity_.class);
															context.startActivity(intent);
														}
													}
												};
								//设置监听事件
								MyAlertDialog.setLeftRightListener(leftListener, rightListener);
//								LayoutInflater inflaterDl = LayoutInflater.from(context);
//								  LinearLayout  myview = (LinearLayout)inflaterDl.inflate(R.layout.my_alertdialog, null );
//						         //对话框
//						         final Dialog dialog = new AlertDialog.Builder(context).create();
//						         dialog.show();
//						         dialog.getWindow().setContentView(myview);
//						         
//						         //继续购物
//						         myview.findViewById(R.id.continue_shopping).setOnClickListener(new OnClickListener() {
//									
//									@Override
//									public void onClick(View arg0) {
//										if(dialog.isShowing()){
//											dialog.dismiss();
//										}
//									}
//								});
//						         
//						        //去购物车 
//						         myview.findViewById(R.id.go2cart).setOnClickListener(new OnClickListener() {
//										
//										@Override
//										public void onClick(View arg0) {
//												dialog.dismiss();
//												Intent  intent=new Intent(context,CartActivity_.class);
//												context.startActivity(intent);
//										}
//									});
							}else{
								//失败
								ToastUtils.show(context, rsbEntity.getMsg());
							}
						}else{
							ToastUtils.show(context, R.string.network_unavailable);
						}
					}
					
					@Override
					public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
						
					}
				});
	}
}
